package com.hy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hy.common.Result;
import com.hy.entity.Exchange;
import com.hy.service.ExchangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/exchanges")
public class ExchangeController {

    @Autowired
    private ExchangeService exchangeService;

    /**
     * 获取用户兑换记录
     */
    @GetMapping
    public Result<Page<Exchange>> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        return Result.success(exchangeService.getUserExchanges(userId, current, size));
    }

    /**
     * 获取兑换记录详情
     */
    @GetMapping("/{id}")
    public Result<Exchange> getById(@PathVariable Long id) {
        return Result.success(exchangeService.getById(id));
    }

    /**
     * 创建兑换记录
     */
    @PostMapping
    public Result<Boolean> create(@RequestParam Long productId, @RequestParam String address, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        return Result.success(exchangeService.create(userId, productId, address));
    }

    /**
     * 取消兑换
     */
    @PostMapping("/{id}/cancel")
    public Result<Boolean> cancel(@PathVariable Long id, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        return Result.success(exchangeService.cancel(id, userId));
    }
} 
